﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Darwin.Source.Particle_Systems;

using SunXNA.RainbowXNA;

using Sound;

namespace Darwin.Source.Components.World.Entities.BuildingTypes
{
    class CTurret : CBuilding
    {
        const float Height = 2.82f;
        const float Width = 2.5f;

        const float ShootRange = 10.5f;   // Shoot range
        const float ShootRate = 500f;    // Shoot rate in milliseconds
        const int DamagesInfliged = 100; // damages infliged for each shoot

        float last_shoot;

        public override bool Initialise(CRainbowXNA.RenderObjectHandleType handle)
        {
            last_shoot = 0;
            m_instance = Singleton<CRainbowXNA>.Instance.CreateRenderInstance(handle);

            return true;
        }

        public override void DoUpdate(Microsoft.Xna.Framework.GameTime gameTime, World w)
        {
            if (gameTime.TotalGameTime.TotalMilliseconds - last_shoot > ShootRate)    // We can reshoot
            {
                foreach (CEntity it_entities in w.m_entityDictionary.Values)    // For all entities
                {
                    if (it_entities.teamSide != this.teamSide)    // If the entity is not in the same time
                    {
                        if (Vector3.Distance(it_entities.Position, this.Position) < ShootRange)   // If the entity is into the range
                        {
                            // Shoooooooooot !!!!!!!!!!!!!!!
                            it_entities.Health -= DamagesInfliged;

                            SoundManager.getSoundManager().playSound(3);
                            
                            Singleton<Darwin.Source.Particle_Systems.CParticleManager>.Instance.ActivateExplosionSystem(it_entities.Position);

                            // Update the last shoot time
                            last_shoot = (float)gameTime.TotalGameTime.TotalMilliseconds;

                            return;
                        }
                    }
                }
            }
        }
    }
}
